<template>
  <div class="menu-search-dialog">
    <i @click="handleOpen" class="iconfont icon-search toolBar-icon"></i>
    <el-dialog v-model="isShowSearch" destroy-on-close :modal="false" :show-close="false" fullscreen @click="closeSearch">
      <el-autocomplete
        v-model="searchMenu"
        ref="menuInputRef"
        value-key="path"
        placeholder="菜单搜索 ：支持菜单名称、路径"
        :fetch-suggestions="searchMenuList"
        @select="handleClickMenu"
        @click.stop
      >
        <template #prefix>
          <el-icon>
            <Search />
          </el-icon>
        </template>
        <template #default="{ item }">
          <el-icon>
            <component :is="item.icon"></component>
          </el-icon>
          <span> {{ item.title }} </span>
        </template>
      </el-autocomplete>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, nextTick } from "vue";
import { Search } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";

const router = useRouter();

// 模拟菜单数据
const menuList = [
  { path: '/home', title: '首页', icon: 'House' },
  { path: '/system/accountManage', title: '用户管理', icon: 'User' },
  { path: '/system/roleManage', title: '角色管理', icon: 'UserFilled' },
  { path: '/system/menuManage', title: '菜单管理', icon: 'Menu' },
  { path: '/system/departmentManage', title: '部门管理', icon: 'OfficeBuilding' },
  { path: '/system/dictManage', title: '字典管理', icon: 'Document' },
  { path: '/system/systemLog', title: '系统日志', icon: 'DocumentCopy' },
  { path: '/echarts', title: '图表分析', icon: 'TrendCharts' },
  { path: '/map', title: '地图功能', icon: 'Location' },
  { path: '/productionScreen', title: '生产大屏', icon: 'Monitor' },
  { path: '/workflow', title: '工作流', icon: 'Connection' },
  { path: '/editor', title: '富文本编辑器', icon: 'Edit' },
  { path: '/TForm', title: 'TForm组件', icon: 'Document' },
  { path: '/TTable', title: 'TTable组件', icon: 'Grid' },
  { path: '/TModuleForm', title: 'TModuleForm组件', icon: 'Document' },
  { path: '/TQueryCondition', title: 'TQueryCondition组件', icon: 'Search' },
  { path: '/TSelectTable', title: 'TSelectTable组件', icon: 'Grid' },
  { path: '/tuiplus', title: 'TUI Plus', icon: 'Star' },
  { path: '/auth/button', title: '按钮权限', icon: 'Key' },
  { path: '/auth/menu', title: '菜单权限', icon: 'Menu' },
  { path: '/scss', title: 'SCSS示例', icon: 'Document' }
];

const searchMenuList = (queryString: string, cb: Function) => {
  const results = queryString ? menuList.filter(filterNodeMethod(queryString)) : menuList;
  cb(results);
};

// 打开搜索框
const isShowSearch = ref(false);
const menuInputRef = ref();
const searchMenu = ref("");
const handleOpen = () => {
  isShowSearch.value = true;
  nextTick(() => {
    setTimeout(() => {
      menuInputRef.value.focus();
    });
  });
};

// 搜索窗关闭
const closeSearch = () => {
  isShowSearch.value = false;
};

// 筛选菜单
const filterNodeMethod = (queryString: string) => {
  return (restaurant: any) => {
    return (
      restaurant.path.toLowerCase().indexOf(queryString.toLowerCase()) > -1 ||
      restaurant.title.toLowerCase().indexOf(queryString.toLowerCase()) > -1
    );
  };
};

// 点击菜单跳转
const handleClickMenu = (menuItem: any) => {
  searchMenu.value = "";
  router.push(menuItem.path);
  closeSearch();
};
</script>

<script lang="ts">
export default {
  name: 'SearchMenu'
};
</script>

<style scoped lang="scss">
.menu-search-dialog {
  :deep(.el-dialog) {
    background-color: rgb(0 0 0 / 50%);
    border-radius: 0 !important;
    box-shadow: unset !important;
    .el-dialog__header {
      border-bottom: none !important;
    }
  }
  :deep(.el-autocomplete) {
    position: absolute;
    top: 100px;
    left: 50%;
    width: 550px;
    transform: translateX(-50%);
    .el-input__wrapper {
      background-color: var(--el-bg-color);
    }
  }
}
.el-autocomplete__popper {
  .el-icon {
    position: relative;
    top: 2px;
    font-size: 16px;
  }
  span {
    margin: 0 0 0 10px;
    font-size: 14px;
  }
}

.toolBar-icon {
  cursor: pointer;
  font-size: 18px !important;
  color: var(--el-text-color-primary) !important;
  transition: color 0.3s;
}

.toolBar-icon:hover {
  color: var(--el-color-primary) !important;
}
</style>
